﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using STT.Business;
using STT.Entities;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Text;
using STT.Layer.Connection;

namespace STT.Admin.UserControls
{
    public partial class ucDangNhap : System.Web.UI.UserControl
    {
        public bool chonSuaClick;
        ToolBox tb = new ToolBox();
        BTbDangNhap dn = new BTbDangNhap();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDangNhap();
            LoadDDL();  
        }
        void LoadDangNhap()
        {
            gvDangNhap.AutoGenerateColumns = false;
            gvDangNhap.DataSource = BTbDangNhap.SelectAll();
            gvDangNhap.DataBind();
        }
        bool KiemTraDuLieu()
        {
            return BTbDangNhap.KiemTraDuLieu(new ETbDangNhap
            {
                TenDangNhap=txtTenDangNhap.Text  
            });
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void gvDangNhap_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDangNhap.EditIndex = e.NewEditIndex;
            gvDangNhap.DataBind();
        }

        protected void gvDangNhap_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int MaDN = int.Parse(gvDangNhap.DataKeys[e.RowIndex].Value.ToString());

                BTbKhoaHoc.Delete(MaDN);
                LoadDangNhap();
                Session["action"] = 3;
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến tài khoản này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void gvDangNhap_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDangNhap.EditIndex = -1;
            gvDangNhap.DataBind();
        }
        private void LoadDDL()
        {
            Methods.DonViGiangVien(ref ddldonvi, Session["TenDN"].ToString());
            Methods.ChucVu(ref ddlchucvu);
            Methods.Quyen(ref ddlquyen);

        }
        protected void btThemMoi_Click(object sender, EventArgs e)
        {
            if (!KiemTraDuLieu())
            {
                try
                {

                    if (tb.GetDataTableById2("TbGiangVien_GetMaGV", txtTenDangNhap.Text).Rows.Count > 0)
                    {
                        ThongBao("Tên đăng nhập đã tồn tại..!");
                    }
                    else
                    {
                        if (ddldonvi.SelectedValue.ToString() == "-1" || ddlchucvu.SelectedValue.ToString() == "-1")
                        {
                            ThongBao("Bạn chưa chọn đơn vị hoặc chức vụ");
                            btThemMoi.Visible = true;
                            btSua.Visible = false;
                        }
                        else
                        {
                            if (ddlquyen.SelectedValue.ToString() != "")
                            {
                                ETbDangNhap dn1 = new ETbDangNhap();
                                dn1.TenDangNhap = txtTenDangNhap.Text;
                                dn1.KhoaTaiKhoan = Convert.ToInt32(ddlKhoaTK.SelectedValue.ToString());
                                dn1.MatKhau = dn.GetMD5(txtMatKhau.Text + dn.Radomchar());
                                dn1.Quyen = ddlquyen.SelectedValue.ToString();
                                dn1.RadomPass = dn.Radomchar();
                                BTbDangNhap.InsertDangNhap(dn1);
                                ETbGiangVien tttk = new ETbGiangVien();
                                tttk.MaGV = txtTenDangNhap.Text;
                                tttk.TenGV = txthoten.Text;
                                tttk.Sdt = txtsdt.Text;
                                tttk.MaDonVi = Convert.ToInt32(ddldonvi.SelectedValue.ToString());
                                tttk.MaChucVu = Convert.ToInt32(ddlchucvu.SelectedValue.ToString());
                                tttk.Anh = "";
                                BTbDangNhap.InsertToTbGiangVien(tttk);
                                ThongBao("Thêm mới thành công");
                                LoadDangNhap();
                            }
                            else
                            {
                                ThongBao("Bạn chưa chọn quyền của tài khoản..!");
                            }
                            Huy();
                            btThemMoi.Visible = true;
                            btSua.Visible = false;
                            LoadDDL();
                            Session["action"] = 1;
                        }
                    }
                }
                catch { }
            }
            else
            {
                ThongBao("Tài khoản này đã tồn tại!");
            }
        }
        private void Huy()
        {
            txtTenDangNhap.Text = "";
            txthoten.Text = "";
            txtMatKhau.Text = "";
            txtsdt.Text = "";
            txtXacNhanMK.Text = "";
            ckqlgv.Checked = false;
            ckqlkhoalop.Checked = false;
            ckqlchung.Checked = false;
            ckqlhv.Checked = false;
            cktimkiem.Checked = false;
            ckthongke.Checked = false;
            ckqlhethong.Checked = false;
            ckqltintuc.Checked = false;
        }
        protected void lbXuatDuLieu_Click(object sender, EventArgs e)
        {

        }
        // Phân trang
        protected void gvDangNhap_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDangNhap.PageIndex = e.NewPageIndex;
            gvDangNhap.DataBind();
        }
        public static String maDN;
        public static String TenDN;
        protected void gvDangNhap_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChonSua")
            {
                pnthemtk.Visible = true;
                btSua.Visible = true;
                btThemMoi.Visible = false;
                int index = Convert.ToInt32(e.CommandArgument);
                maDN = gvDangNhap.DataKeys[index].Value.ToString();
                TenDN = gvDangNhap.Rows[index].Cells[0].Text.Trim();
                LoadDataSua();
                txtTieuDe.InnerText = "Sửa khóa học";
                DataTable infodn = tb.GetDataTableById2("TbDangNhap_ByMaDN", maDN);
                txtTenDangNhap.Text = infodn.Rows[0]["TenDN"].ToString() ;
                txthoten.Text = infodn.Rows[0]["TenGV"].ToString();
                txtsdt.Text = infodn.Rows[0]["Sdt"].ToString().Trim();
                ddlquyen.SelectedValue = infodn.Rows[0]["MaQuyen"].ToString();
                ddlKhoaTK.SelectedValue = infodn.Rows[0]["KhoaTaiKhoan"].ToString();
                ddlchucvu.SelectedValue = infodn.Rows[0]["Machucvu"].ToString();
                ddldonvi.SelectedValue = infodn.Rows[0]["MaDonvi"].ToString();
                DataTable cnquyen = tb.GetDataTableById("TbDangNhap_GetCNQuyenByMaQuyen", infodn.Rows[0]["Quyen"].ToString());
                for (int i = 0; i < cnquyen.Rows.Count; i++)
                {
                    if (cnquyen.Rows[i]["MaCN"].ToString() == "1") ckqlhv.Checked = true;
                    else if (cnquyen.Rows[i]["MaCN"].ToString() == "2") ckqlgv.Checked = true;
                    else if (cnquyen.Rows[i]["MaCN"].ToString() == "3") ckqlkhoalop.Checked = true;
                    else if (cnquyen.Rows[i]["MaCN"].ToString() == "4") ckqlchung.Checked = true;
                    else if (cnquyen.Rows[i]["MaCN"].ToString() == "5") ckqltintuc.Checked = true;
                    else if (cnquyen.Rows[i]["MaCN"].ToString() == "6") cktimkiem.Checked = true;
                    else if (cnquyen.Rows[i]["MaCN"].ToString() == "7") ckthongke.Checked = true;
                    else if (cnquyen.Rows[i]["MaCN"].ToString() == "8") ckqlhethong.Checked = true;
                }

            }
        }
        private void LoadDataSua()
        {
            mk.Visible = false;
            xnmk.Visible = false;
            hoten.Visible = true;
            txtTenDangNhap.Enabled = false;
            txtsdt.Enabled = false;
            txthoten.Enabled = false;
            tbchucnang.Enabled = false;
        }
        private void LoadDataThem()
        {
            mk.Visible = true;
            xnmk.Visible = true;
            hoten.Visible = true;
            txtTenDangNhap.Enabled = true;
            txtsdt.Enabled = true;
            txthoten.Enabled = true;
            tbchucnang.Enabled = false;
        }
        protected void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                if (!KiemTraDuLieu())
                {
                    BTbDangNhap.Update_PhanQuyen(new ETbDangNhap
                    {
                        MaDN = int.Parse(maDN),
                        Quyen = ddlquyen.SelectedValue.ToString(),
                        KhoaTaiKhoan = int.Parse(ddlKhoaTK.SelectedValue.ToString())
                    });
                    BTbDangNhap.Update_ChucVuDonVi(new ETbGiangVien
                    {
                        MaGV = TenDN,
                        MaChucVu = int.Parse(ddlchucvu.SelectedValue.ToString()),
                        MaDonVi = int.Parse(ddldonvi.SelectedValue.ToString())
                    });
                    Session["action"] = 2;
                    LoadDangNhap();
                }
            }
            catch   {}
        }

        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {

            gvDangNhap.DataSource = BTbDangNhap.SearchDangNhap(new ETbDangNhap
            {
                TenDangNhap = txtTimKiem.Text
            });

            gvDangNhap.DataBind();

        }

        protected void btImport_Click(object sender, EventArgs e)
        {
          
        }
        protected void btThemExcel_Click(object sender, EventArgs e)
        {

        }

        protected void btThem_ServerClick(object sender, EventArgs e)
        {
            btThemMoi.Visible = true;
            pnthemtk.Visible = true;
            Huy();
            LoadDDL();
            LoadDataThem();
            btSua.Visible = false;
        }
        private void HuyCheckbox()
        {
            ckqlgv.Checked = false;
            ckqlkhoalop.Checked = false;
            ckqlchung.Checked = false;
            ckqlhv.Checked = false;
            cktimkiem.Checked = false;
            ckthongke.Checked = false;
            ckqlhethong.Checked = false;
            ckqltintuc.Checked = false;
        }
        protected void ddlquyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlquyen.SelectedValue.ToString() == "-1")
            {
                ThongBao("Bạn phải chọn quyền.");
            }
            else
            {
                DataTable dt = tb.GetDataTableById("TbChucNang_Quyen_GetByMaQuyen", ddlquyen.SelectedValue.ToString());
                HuyCheckbox();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["MaCN"].ToString() == "1") ckqlhv.Checked = true;
                    else if (dt.Rows[i]["MaCN"].ToString() == "2") ckqlgv.Checked = true;
                    else if (dt.Rows[i]["MaCN"].ToString() == "3") ckqlkhoalop.Checked = true;
                    else if (dt.Rows[i]["MaCN"].ToString() == "4") ckqlchung.Checked = true;
                    else if (dt.Rows[i]["MaCN"].ToString() == "5") ckqltintuc.Checked = true;
                    else if (dt.Rows[i]["MaCN"].ToString() == "6") cktimkiem.Checked = true;
                    else if (dt.Rows[i]["MaCN"].ToString() == "7") ckthongke.Checked = true;
                    else if (dt.Rows[i]["MaCN"].ToString() == "8") ckqlhethong.Checked = true;
                }
                tbchucnang.Enabled = false;
            }
        }

        protected void btnhuy_Click(object sender, EventArgs e)
        {
            pnthemtk.Visible = false;
        }    
    }
}